/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.Order1;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.PreexistingEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.City;
import Entity.Accounts;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class Order1JpaController implements Serializable {

    public Order1JpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Order1 order1) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            City cityId = order1.getCityId();
            if (cityId != null) {
                cityId = em.getReference(cityId.getClass(), cityId.getCityId());
                order1.setCityId(cityId);
            }
            Accounts accId = order1.getAccId();
            if (accId != null) {
                accId = em.getReference(accId.getClass(), accId.getAccId());
                order1.setAccId(accId);
            }
            em.persist(order1);
            if (cityId != null) {
                cityId.getOrder1List().add(order1);
                cityId = em.merge(cityId);
            }
            if (accId != null) {
                accId.getOrder1List().add(order1);
                accId = em.merge(accId);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findOrder1(order1.getOrderId()) != null) {
                throw new PreexistingEntityException("Order1 " + order1 + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Order1 order1) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Order1 persistentOrder1 = em.find(Order1.class, order1.getOrderId());
            City cityIdOld = persistentOrder1.getCityId();
            City cityIdNew = order1.getCityId();
            Accounts accIdOld = persistentOrder1.getAccId();
            Accounts accIdNew = order1.getAccId();
            if (cityIdNew != null) {
                cityIdNew = em.getReference(cityIdNew.getClass(), cityIdNew.getCityId());
                order1.setCityId(cityIdNew);
            }
            if (accIdNew != null) {
                accIdNew = em.getReference(accIdNew.getClass(), accIdNew.getAccId());
                order1.setAccId(accIdNew);
            }
            order1 = em.merge(order1);
            if (cityIdOld != null && !cityIdOld.equals(cityIdNew)) {
                cityIdOld.getOrder1List().remove(order1);
                cityIdOld = em.merge(cityIdOld);
            }
            if (cityIdNew != null && !cityIdNew.equals(cityIdOld)) {
                cityIdNew.getOrder1List().add(order1);
                cityIdNew = em.merge(cityIdNew);
            }
            if (accIdOld != null && !accIdOld.equals(accIdNew)) {
                accIdOld.getOrder1List().remove(order1);
                accIdOld = em.merge(accIdOld);
            }
            if (accIdNew != null && !accIdNew.equals(accIdOld)) {
                accIdNew.getOrder1List().add(order1);
                accIdNew = em.merge(accIdNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = order1.getOrderId();
                if (findOrder1(id) == null) {
                    throw new NonexistentEntityException("The order1 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Order1 order1;
            try {
                order1 = em.getReference(Order1.class, id);
                order1.getOrderId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The order1 with id " + id + " no longer exists.", enfe);
            }
            City cityId = order1.getCityId();
            if (cityId != null) {
                cityId.getOrder1List().remove(order1);
                cityId = em.merge(cityId);
            }
            Accounts accId = order1.getAccId();
            if (accId != null) {
                accId.getOrder1List().remove(order1);
                accId = em.merge(accId);
            }
            em.remove(order1);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Order1> findOrder1Entities() {
        return findOrder1Entities(true, -1, -1);
    }

    public List<Order1> findOrder1Entities(int maxResults, int firstResult) {
        return findOrder1Entities(false, maxResults, firstResult);
    }

    private List<Order1> findOrder1Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Order1 as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Order1 findOrder1(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Order1.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrder1Count() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Order1 as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
